﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;


public class AESEncrypt
{
    /// <summary>
    /// 有密码的AES加密 
    /// </summary>
    /// <param name="text">加密字符</param>
    /// <param name="password">加密的密码</param>
    /// <param name="iv">密钥</param>
    /// <returns></returns>
    public static string Encrypt(string text, string password, string iv)
    {
        RijndaelManaged rijndaelCipher = new RijndaelManaged();

        rijndaelCipher.Mode = CipherMode.CBC;

        rijndaelCipher.Padding = PaddingMode.Zeros;

        rijndaelCipher.KeySize = 256;

        rijndaelCipher.BlockSize = 128;

        byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);

        byte[] keyBytes = new byte[32];

        int len = pwdBytes.Length;

        if (len > keyBytes.Length) len = keyBytes.Length;

        System.Array.Copy(pwdBytes, keyBytes, len);

        rijndaelCipher.Key = keyBytes;


        byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
        rijndaelCipher.IV = ivBytes;

        ICryptoTransform transform = rijndaelCipher.CreateEncryptor();

        //int count = 0;
        //if (int.Parse(text.Length.ToString()) % 16 != 0)
        //{
        //    count = 16 - int.Parse(text.Length.ToString()) % 16;

        //}

        byte[] plainText = Encoding.UTF8.GetBytes(text);


        //byte[] cipherBytes = new byte[plainText.Length + count];

        byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);

        //for (int i = 0; i < count; i++)
        //{
        //    //cipherBytes[(cipherBytes.Length - 1) + i] = 0;
        //}

        return Convert.ToBase64String(cipherBytes);

    }

    /// <summary>
    /// 随机生成密钥128、192 和 256 位密钥
    /// </summary>
    /// <returns></returns>
    public static string GetIv(int n)
    {
        char[] arrChar = new char[]{
           'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x',
           '0','1','2','3','4','5','6','7','8','9',
           'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z'
          };

        StringBuilder num = new StringBuilder();

        Random rnd = new Random(DateTime.Now.Millisecond);
        for (int i = 0; i < n; i++)
        {
            num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());

        }

        return num.ToString();
    }

    /// <summary>
    /// AES解密
    /// </summary>
    /// <param name="text"></param>
    /// <param name="password"></param>
    /// <param name="iv"></param>
    /// <returns></returns>
    public static string Decrypt(string text, string password, string iv)
    {
        RijndaelManaged rijndaelCipher = new RijndaelManaged();

        rijndaelCipher.Mode = CipherMode.CBC;

        rijndaelCipher.Padding = PaddingMode.Zeros;

        rijndaelCipher.KeySize = 256;

        rijndaelCipher.BlockSize = 128;


        byte[] encryptedData = Convert.FromBase64String(text);

        byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);

        byte[] keyBytes = new byte[32];

        int len = pwdBytes.Length;

        if (len > keyBytes.Length) len = keyBytes.Length;

        System.Array.Copy(pwdBytes, keyBytes, len);

        rijndaelCipher.Key = pwdBytes;

        byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
        rijndaelCipher.IV = ivBytes;

        ICryptoTransform transform = rijndaelCipher.CreateDecryptor();

        byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);

        return Encoding.UTF8.GetString(plainText);

    }


}

